export default function updateNodeElement(newElement, virtualDOM, oldVDOM = {}) {
    const { props } = virtualDOM
    const oldProps = oldVDOM.props || {}
    Object.keys(props).forEach(attrName => {
        const newPropsValue = props[attrName]
        const oldPropsValue = oldProps && oldProps[attrName]

        if (newPropsValue !== oldPropsValue) {
            if (attrName.startsWith("on")) {
                let eventName = attrName.toLowerCase().slice(2)
                newElement.addEventListener(eventName, newPropsValue)
                oldPropsValue && newElement.removeEventListener(eventName, oldPropsValue)
            } else if (attrName === "checked" || attrName === "value") {
                newElement[attrName] = newPropsValue
            } else if (attrName !== "children") {
                if (attrName === "className") {
                    newElement.setAttribute("class", newPropsValue)
                } else {
                    newElement.setAttribute(attrName, newPropsValue)
                }
            }
        }
    })

    Object.keys(oldProps).forEach(attrName => {
        const newPropsValue = props[attrName]
        const oldPropsValue = oldProps[attrName]
        if (!newPropsValue) {
            if (attrName.startsWith("on")) {
                const eventName = attrName.toLowerCase().slice(2)
                newElement.removeEventListener(eventName, oldPropsValue)
            } else if (attrName !== "children") {
                newElement.removeAttribute(attrName)
            }
        }
    })
}